package com.food.restaurantpro.database.tables;

import java.util.ArrayList;
import java.util.List;

import com.food.restaurantpro.RestaurantApp;
import com.food.restaurantpro.database.RestaurantAppContentProvider;
import com.food.restaurantpro.objects.Promotion;

import android.content.ContentResolver;
import android.content.ContentValues;
import android.database.Cursor;
import android.util.Log;

public class PromotionTable {
	public static final String TAG = "PromotionTable";
	
	public static final String TABLE_NAME = "promotions";
	
	public static final String PROMOTION_ID = "promotionID";
	public static final String PROMOTION_NAME = "promotionName";
	public static final String PROMOTION_DESCRIPTION = "promotionDescription";
	public static final String PROMOTION_SOLOGAN = "promotionSologan";
	public static final String PROMOTION_DISCOUNT = "promotionDiscount";
	public static final String PROMOTION_IMAGE_SLIDE = "promotionImageSlide";
	public static final String PROMOTION_IMAGE_THUMBNAIL = "promotionImageThumbnail";
	public static final String PROMOTION_IMAGE_MAIN = "promotionImageMain";
	public static final String PROMOTION_DATE = "promotionDate";
	public static final String PROMOTION_TIME = "promotionTime";

	public static final String SQL_EXCUTE = "CREATE TABLE " + TABLE_NAME + "("
			+ PROMOTION_ID + " text primary key not null, "
			+ PROMOTION_NAME + " text not null, "
			+ PROMOTION_DESCRIPTION + " text not null, "
			+ PROMOTION_SOLOGAN + " text not null, "
			+ PROMOTION_DISCOUNT + " text not null, "
			+ PROMOTION_IMAGE_SLIDE + " text not null, "
			+ PROMOTION_IMAGE_THUMBNAIL + " text not null, "
			+ PROMOTION_IMAGE_MAIN + " text not null, "
			+ PROMOTION_DATE + " text not null, "
			+ PROMOTION_TIME + " text not null);";
	
	public static void deleteAll()
	{
		ContentResolver resolver = RestaurantApp.CURRENT_CONTEXT.getContentResolver();
		resolver.delete(RestaurantAppContentProvider.PROMOTION_CONTENT_URI, null, null);
	}
	/**
	 * @param listPromotions list promotions to insert to db
	 * @return the number of newly created rows. 
	 */
	public static int insertListPromotions(List<Promotion> listPromotions) {
		if(listPromotions != null && listPromotions.size() > 0) {
			int size = listPromotions.size();
			//show progress
			ContentValues[] allValues = new ContentValues[size];
			for (int i = 0; i < size; i++) {
				ContentValues value = new ContentValues();
				Promotion promotion = listPromotions.get(i);
				value.put(PROMOTION_ID, promotion.getPromotionId());
				value.put(PROMOTION_NAME, promotion.getPromotionName());
				value.put(PROMOTION_DESCRIPTION, promotion.getPromotionDescription());
				value.put(PROMOTION_SOLOGAN, promotion.getPromotionSologan());
				value.put(PROMOTION_DISCOUNT, promotion.getPromotionDiscount());
				value.put(PROMOTION_IMAGE_SLIDE, promotion.getPromotionImageSlide());
				value.put(PROMOTION_IMAGE_THUMBNAIL, promotion.getPromotionImageThumbnail());
				value.put(PROMOTION_IMAGE_MAIN, promotion.getPromotionImageMain());
				value.put(PROMOTION_DATE, promotion.getPromotionDate());
				value.put(PROMOTION_TIME, promotion.getPromotionTime());
				
				allValues[i] = value;
			}
			ContentResolver resolver = RestaurantApp.CURRENT_CONTEXT.getContentResolver();
			int rowadd = resolver.bulkInsert(RestaurantAppContentProvider.PROMOTION_CONTENT_URI, allValues);
			return rowadd;
			//clode progress
		}
		return 0;
	}
	
	public static ArrayList<String> getAllPromotionID() {

		ContentResolver resolver = RestaurantApp.CURRENT_CONTEXT.getContentResolver();
		ArrayList<String> arrayList = new ArrayList<String>();
		String[] columns = new String[]{PROMOTION_ID};
		Cursor cursor = resolver.query(RestaurantAppContentProvider.PROMOTION_CONTENT_URI, columns, null, null, PROMOTION_NAME + " ASC");
		if(cursor != null){
			if(cursor.getCount()>0){
				try {
					while (!cursor.isLast()) {
						cursor.moveToNext();
						String promotionId = cursor.getString(cursor.getColumnIndex(PROMOTION_ID));
						arrayList.add(promotionId);
					}
				} catch (Exception e) {
					// TODO: handle exception
					Log.e(TAG, "ERROR getAllPromotions()", e);
					arrayList = null;
				}
			}
			cursor.close();
		}
		return arrayList;
	}
	
	public static ArrayList<Promotion> getAllPromotions()
	{
		ContentResolver resolver = RestaurantApp.CURRENT_CONTEXT.getContentResolver();
		ArrayList<Promotion> arrayList = new ArrayList<Promotion>();
		Cursor cursor = resolver.query(RestaurantAppContentProvider.PROMOTION_CONTENT_URI, null, null, null, PROMOTION_NAME + " ASC");
		if(cursor != null){
			//cach 1
			/*if(cursor.moveToFirst()) {
				do{
					//TODO
				} while(cursor.moveToNext());
			}*/
			//cach 2
			if(cursor.getCount()>0){
				try {
					while (!cursor.isLast()) {
						cursor.moveToNext();
						Promotion promotion = new Promotion();
						
						String promotionId = cursor.getString(cursor.getColumnIndex(PROMOTION_ID));
						promotion.setPromotionId(promotionId);
						
						String promotionName = cursor.getString(cursor.getColumnIndex(PROMOTION_NAME));
						promotion.setPromotionName(promotionName);
						
						String promotionDescription = cursor.getString(cursor.getColumnIndex(PROMOTION_DESCRIPTION));
						promotion.setPromotionDescription(promotionDescription);
						
						String promotionSologan = cursor.getString(cursor.getColumnIndex(PROMOTION_SOLOGAN));
						promotion.setPromotionSologan(promotionSologan);
						
						String promotionDiscount = cursor.getString(cursor.getColumnIndex(PROMOTION_DISCOUNT));
						promotion.setPromotionDiscount(promotionDiscount);
						
						String promotionImageSlide = cursor.getString(cursor.getColumnIndex(PROMOTION_IMAGE_SLIDE));
						promotion.setPromotionImageSlide(promotionImageSlide);
						
						String promotionImageThumbnail = cursor.getString(cursor.getColumnIndex(PROMOTION_IMAGE_THUMBNAIL));
						promotion.setPromotionImageThumbnail(promotionImageThumbnail);
						
						String promotionImageMain = cursor.getString(cursor.getColumnIndex(PROMOTION_IMAGE_MAIN));
						promotion.setPromotionImageMain(promotionImageMain);
						
						String promotionDate = cursor.getString(cursor.getColumnIndex(PROMOTION_DATE));
						promotion.setPromotionDate(promotionDate);
						
						String promotionTime = cursor.getString(cursor.getColumnIndex(PROMOTION_TIME));
						promotion.setPromotionTime(promotionTime);
						
						arrayList.add(promotion);
					}
				} catch (Exception e) {
					// TODO: handle exception
					Log.e(TAG, "ERROR getAllPromotions()", e);
				}
			}
			cursor.close();
		}
		return arrayList;
	}
	
	public static void insertPromotion(Promotion promotion) {
		if(promotion != null) {
			ContentValues value = new ContentValues();
			value.put(PROMOTION_ID, promotion.getPromotionId());
			value.put(PROMOTION_NAME, promotion.getPromotionName());
			value.put(PROMOTION_DESCRIPTION, promotion.getPromotionDescription());
			value.put(PROMOTION_SOLOGAN, promotion.getPromotionSologan());
			value.put(PROMOTION_DISCOUNT, promotion.getPromotionDiscount());
			value.put(PROMOTION_IMAGE_SLIDE, promotion.getPromotionImageSlide());
			value.put(PROMOTION_IMAGE_THUMBNAIL, promotion.getPromotionImageThumbnail());
			value.put(PROMOTION_IMAGE_MAIN, promotion.getPromotionImageMain());
			value.put(PROMOTION_DATE, promotion.getPromotionDate());
			value.put(PROMOTION_TIME, promotion.getPromotionTime());
				
			ContentResolver resolver = RestaurantApp.CURRENT_CONTEXT.getContentResolver();
			resolver.insert(RestaurantAppContentProvider.PROMOTION_CONTENT_URI, value);
		}
	}
	
	public static int updatePromotionByID(Promotion promotion) {
		if(promotion != null) {
			ContentValues value = new ContentValues();
			value.put(PROMOTION_ID, promotion.getPromotionId());
			value.put(PROMOTION_NAME, promotion.getPromotionName());
			value.put(PROMOTION_DESCRIPTION, promotion.getPromotionDescription());
			value.put(PROMOTION_SOLOGAN, promotion.getPromotionSologan());
			value.put(PROMOTION_DISCOUNT, promotion.getPromotionDiscount());
			value.put(PROMOTION_IMAGE_SLIDE, promotion.getPromotionImageSlide());
			value.put(PROMOTION_IMAGE_THUMBNAIL, promotion.getPromotionImageThumbnail());
			value.put(PROMOTION_IMAGE_MAIN, promotion.getPromotionImageMain());
			value.put(PROMOTION_DATE, promotion.getPromotionDate());
			value.put(PROMOTION_TIME, promotion.getPromotionTime());
				
			ContentResolver resolver = RestaurantApp.CURRENT_CONTEXT.getContentResolver();
			int rowUpdate = resolver.update(RestaurantAppContentProvider.PROMOTION_CONTENT_URI, value, PROMOTION_ID + "=?", new String[] {promotion.getPromotionId()});
			return rowUpdate;
		}
		return -1;
	}
	
	public static void deletePromotionByID(String promotionId)
	{
		ContentResolver resolver = RestaurantApp.CURRENT_CONTEXT.getContentResolver();
		resolver.delete(RestaurantAppContentProvider.PROMOTION_CONTENT_URI, PROMOTION_ID + "=?", new String[] {promotionId});
	}
}
